﻿/*
 * Twipler - Twitter Web Client with features.
 *  
 *  Copyright (C) 2009, 2010. Ian Quigley
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details. 
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */


using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

using Twipler.App.Parse.Code;
using Twipler.App.Render.Tree;
using Twipler.Data.Entity.Twipler;
using Twipler.WebApp;

namespace Settings.Sandbox
{
    [RequireUser(VisitorRole.Registered)]
    public partial class Code : System.Web.UI.Page
    {
        private string[] Description = new string[]
                                           {
                                               "Branch If",
                                               "Branch",
                                               "Wild.Add(10, 4) =  14",
                                               "10 + 4 = 14"
                                           };

        private string[] Tests = new string[]
                                     {
                                         "pushi 1\npushi 7\ngei 10\njumpif tothere\npop pushi 1\n:tothere",
                                         "pushi 10\npushi 5\njump tothere\npushi 4\n:tothere\naddi",
                                         "pushi 10\npushi 4\ncall Wild.Add\n",
                                         "pushi 10\npushi 4\naddi"
                                     };


        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                return;

            int test = 0;
            if (Request.QueryString["test"] != null)
                if (int.TryParse(Request.QueryString["test"], out test) == false)
                    test = 0;

            txtBox.Text = Tests[test];

            
            if (test < Description.Count())
                litDescription.Text = Description[test];
            else
                litDescription.Text = "No description";
        }

        protected void btnSub_Click(object sender, EventArgs e)
        {
            try
            {

                CodeParse gen = new CodeParse(txtBox.Text);
                Twipler.App.Machine.Environment env = new Twipler.App.Machine.Environment();

            
                List<Twipler.App.Machine.Instruction> program = gen.ReadInstructions();

                litRender.Text = "";


                foreach (Twipler.App.Parse.ParseError error in gen.Errors)
                    litRender.Text += "Error: " + error.Message + "<br/>";

                if (litRender.Text.Length > 0)
                    return;
            
                object o = env.Execute(program);

                if (o == null)
                    litRender.Text = "null";
                else
                    litRender.Text = o.ToString();
            }
            catch (Exception ex)
            {
                litRender.Text = "<span class=\"error\">Exception!</span><br/>" + ex.Message;
            }

        }

    }
}